package com.Quy.administrator.mybitmapdeal;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private ImageView imageView2;
    private byte[] data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView= (ImageView) findViewById(R.id.imageView);
        imageView2= (ImageView) findViewById(R.id.imageView2);

    }
    public void down(View view){
        new DownTask().execute("http://p4.so.qhimg.com/bdr/_240_/t01d4c378e7c6bfbe69.jpg");

    }
    public class DownTask extends AsyncTask<String,Void,Bitmap[] >{

        @Override
        protected Bitmap[] doInBackground(String... params) {
            byte[] data=getNetWork(params[0]);
            Bitmap[] bms=new Bitmap[2];
            Bitmap bm= BitmapFactory.decodeByteArray(data,0,data.length);
            bms[0]=bm;
            Bitmap bm2=BitMapUtils.getBitMapByte(data,10,10);
            bms[1]=bm2;
            return bms;
        }

        @Override
        protected void onPostExecute(Bitmap[] bitmap) {
            super.onPostExecute(bitmap);
            for(int i=0;i<bitmap.length;i++) {
                Bitmap bm = bitmap[i];
                int size=bm.getRowBytes()*bm.getHeight();
                Log.i("123","bm width-->"+i+bm.getWidth());
                Log.i("123","bm Height-->"+i+bm.getHeight());
                Log.i("123","bm size-->"+i+size);
            }
            imageView.setImageBitmap(bitmap[0]);
            imageView2.setImageBitmap(bitmap[1]);
        }
    }

    public byte[] getNetWork(String web){
        HttpURLConnection con;
        ByteArrayOutputStream bos=null;
        InputStream is=null;
        try {
            con = (HttpURLConnection) new URL(web).openConnection();

            con.setConnectTimeout(3000);
            if(con.getResponseCode()==200){
                is=	con.getInputStream();
                bos=new ByteArrayOutputStream();
                int b=0;
                byte[] buf=new byte[1024];
                while((b=is.read(buf))!=-1){
                    bos.write(buf, 0, b);
                }
                data=bos.toByteArray();

                return data;
            }
        } catch (IOException e) {
            e.printStackTrace();

        }finally{
            try {
                if(bos!=null)
                    bos.close();
                if(is!=null)
                    is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
}
